<?php
namespace app\admin\controller;

use app\index\controller\MallcooApi;
use think\Controller;
use think\Db;
use wechat\Wechat;

//定时任务
class Queue extends Controller
{
    //重启crontab:systemctl restart crond
    public function tasks(){
        //die;
        //查询需要推送的，5分钟之前的
        $time = date('Y-m-d H:i:s',strtotime('-5 minutes'));
        $msg = Db::name('msg')
            ->alias('a')
            ->join('user b','a.user_id=b.id','left')
            ->join('staff c','a.staff_id=c.id','left')
            ->where('need_push',1)
            ->where('a.create_time','lt',$time)
            ->field('a.*,user_name,nick_name,b.mobile as user_mobile,c.mobile as staff_mobile,c.openid,c.nick_name')
            ->select();
		if(!$msg){
			die;
		}
        //dump($msg);die;
        foreach ($msg as $k=>$v){
			$v['content'] = msg($v['content'])[2];
            if($v['send_type'] == 1){
                //发送客服短信
                //短信模板
                $sms_template_info = Db::name('template')->where('scene','noread')->where('type',2)->find();
                if($sms_template_info){
                    $a = (new Common())->sendSms($v['staff_mobile'],$this->replaceParam($v,$sms_template_info['content'],2));
					\think\facade\Log::debug($a);
                }

                //发送公众号消息
                //微信模板
                $wx_template_info = Db::name('template')->where('scene','noread')->where('type',1)->find();
                if($wx_template_info){
                    $b = (new Wechat())->pushMsg($v['openid'],$wx_template_info['temp_id'],$this->replaceParam($v,json_decode($wx_template_info['content'],true),1));
					\think\facade\Log::debug($b);
                }
            }else{
                //发给客户的，app提醒，半夜不提醒
                if((date('H:i:s') < '23:59:59') && (date('H:i:s') > '06:00:00')) {
                    //app模板
                    $app_template_info = Db::name('template')->where('scene','noread')->where('type',3)->find();
                    if($app_template_info){
                        $param = [
                            'MobileList' => [$v['user_mobile']],
                            'Title' => '您有新的未读消息',
                            'Content' => $this->replaceParam($v,$app_template_info['content'],3),
                            'Url' => 'https://kf.sylwtech.cn/index/index/kfOnline'
                        ];
                        $c = (new MallcooApi(config('conf.m_Mallid'),config('conf.m_AppID'),config('conf.m_PublicKey'),config('conf.m_PrivateKey')))->mcAppPush($param);
						\think\facade\Log::debug($c);
                    }
                }
            }
            //设为已推送状态，防止重复推送
            Db::name('msg')->where('id',$v['id'])->setField('need_push',0);
        }
    }

    //根据创建替换参数变量
    public function replaceParam($data,$param,$type){
        $rst = [];
        switch ($type){
            case 1://微信
                foreach($param as $k=>$v){
                    $rst[] = ['color'=>$v['color'],'val'=>repStr($v['val'],$data)];
                }
                break;
            case 2://短信
            case 3://app
                $rst = repStr($param,$data);
                break;
            default:
                return [];
        }

        return $rst;
    }
}
